package com.example.producer;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.MessagingException;
import org.springframework.stereotype.Component;

/**
 * @Classname ProducerService
 * @Description
 * @Author caozhenhao001
 * @Date 2024/4/9
 * @Version
 */
@Slf4j
@Component
public class MessageSender {

    @Autowired
    public final RocketMQTemplate rocketMQTemplate;


    public MessageSender(RocketMQTemplate rocketMQTemplate) {
        this.rocketMQTemplate = rocketMQTemplate;
    }
    public void pushNormalMessage(String topic, Object message) {
        try {
            log.info("发送消息topic:{},  message：{}",topic, message);
            rocketMQTemplate.convertAndSend(topic, JSON.toJSONString(message));
        } catch (MessagingException e) {
            log.info("发送消息失败");
            e.printStackTrace();
        }
    }
}
